package com.jeremyfeinstein.utils.NetUtils;

import com.alibaba.fastjson.JSON;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author : lishan
 * @e-mail : cnlishan@163.com
 * @date : 2021/2/8 9:11 AM
 * @desc :
 */
public class PostJSONRequest<T> extends SGRequest<T, PostJSONRequest> {
    public static final MediaType JSON_MT = MediaType.parse("application/json; charset=utf-8");

    public PostJSONRequest(String url) {
        super(url);

        Logger.e("Request: url = %s", url);
    }

    @Override
    protected okhttp3.Request generateRequest(okhttp3.Request.Builder builder) {
        //post请求表单提交
        Map map = new HashMap<>();
        for (Map.Entry<String, Object> entry : mParams.entrySet()) {
            map.put(entry.getKey(), entry.getValue());
        }

        String param = JSON.toJSONString(map);
        RequestBody requestBody = RequestBody.create(JSON_MT, param);
        Logger.e("Request: param = %s", param);
        okhttp3.Request request = builder.url(mUrl).post(requestBody).build();
        return request;
    }
}
